package com.hunttown.mes.manage.service;

import com.hunttown.mes.common.keys.KeyConstants;
import com.hunttown.mes.common.cache.Cache;
import com.hunttown.mes.rpc.domain.AnalysisRegionDTO;
import com.hunttown.common.domain.Page;
import com.hunttown.mes.rpc.api.AnalysisRegionRpcService;
import com.hunttown.mes.rpc.domain.query.AnalysisRegionDTOQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * created by wangjunfu on 2018-11-26 15:06:35
 */
@Service
public class AnalysisRegionManageService {

    private final static Logger logger = LoggerFactory.getLogger(AnalysisRegionManageService.class);

    private final AnalysisRegionRpcService rpcService;
    private final Cache jedisCache;

    @Autowired
    public AnalysisRegionManageService(AnalysisRegionRpcService rpcService, Cache jedisCache) {
        this.rpcService = rpcService;
        this.jedisCache = jedisCache;
    }

    // 通过ID修改
    public Boolean updateInfoById(AnalysisRegionDTO objDTO) {
        Boolean isNext = rpcService.updateInfoById(objDTO);
        if (isNext) {
            // 刷新缓存
            refreshCacheById(objDTO.getId());
        }
        return isNext;
    }

    // 通过Query修改
    public Boolean updateInfoByQuery(AnalysisRegionDTOQuery objDTOQuery) {
        Boolean isNext = rpcService.updateInfoByQuery(objDTOQuery);

        //单体刷新
        if (isNext && objDTOQuery.getW_id() != null) {
            refreshCacheById(objDTOQuery.getW_id());
        }

        //批量刷新缓存
        if (isNext && objDTOQuery.getIds() != null) {
            List<Integer> list = new ArrayList<>();
            try {
                list = (List<Integer>) objDTOQuery.getIds();
            } catch (Exception e) {
                e.printStackTrace();
            }
            refreshCacheByIdList(list);
        }
        return isNext;
    }

    // 通过ID获取
    public AnalysisRegionDTO getById(Integer id) {
        if (id <= 0) {
            return null;
        }
        return rpcService.selectById(id);
    }

    // 通过Query获取
    public AnalysisRegionDTO getByQuery(AnalysisRegionDTOQuery objDTOQuery) {
        return rpcService.selectObjByQuery(objDTOQuery);
    }

    public Integer getIdByRegionCode(String regionCode) {
        AnalysisRegionDTOQuery query = new AnalysisRegionDTOQuery();
        query.setRegionCode(regionCode);
        AnalysisRegionDTO dto = getByQuery(query);
        return dto == null ? 0 : dto.getId();
    }

    // 获取分页列表
    public Page<AnalysisRegionDTO> getForPage(AnalysisRegionDTOQuery objDTOQuery) {
        return rpcService.getListForPage(objDTOQuery);
    }

    /**
     * 先从缓存中获取数据
     * 如果缓存为空，从数据库中获取，并将获取的数据添加到缓存。
     *
     * @param id 编号id
     * @return
     */
    public AnalysisRegionDTO getFromCacheId(Integer id) {
        if (id == null || id <= 0) {
            return null;
        }

        AnalysisRegionDTO obj = jedisCache.oget(KeyConstants.ANALYSISREGION_INFO_KEY_ID + id, AnalysisRegionDTO.class);
        if (obj == null) {
            obj = getById(id);
            if (obj != null) {
                jedisCache.oset(KeyConstants.ANALYSISREGION_INFO_KEY_ID + id, obj);
            }
        }

        return obj;
    }

    //region 刷新缓存

    /**
     * 刷新缓存
     *
     * @param id id
     */
    private void refreshCacheById(Integer id) {
        if (id == null || id <= 0) {
            return;
        }

        AnalysisRegionDTO obj = getById(id);
        if (obj != null) {
            jedisCache.delete(KeyConstants.ANALYSISREGION_INFO_KEY_ID + id);
        }
    }

    /**
     * 刷新缓存(批量)
     *
     * @param idList
     */
    private void refreshCacheByIdList(List<Integer> idList) {
        if (idList == null || idList.size() == 0) {
            return;
        }

        for (Integer id : idList) {
            refreshCacheById(id);
        }
    }
    //endregion

}